package com.big.merchan.receiver;

import com.big.common.utils.UUIDGenerator;
import com.big.merchan.service.*;
import com.big.model.model.merchandise.MerchandiseOffers;
import com.big.model.model.merchandise.MerchandisePhotoCord;
import com.big.model.vo.merchandise.admin.MerchanSaveVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

import static com.big.common.utils.UUIDGenerator.getUUID7;
import static com.big.common.utils.UUIDGenerator.getUUID9;

@Component
@Service
@Slf4j
public class MerchandiseReceiver {

    /**
     * 优惠方式
     */
    @Autowired
    MerchandiseOffersService merchandiseOffersService;

    /**
     * 详情
     */
    @Autowired
    MerchandiseInfoService merchandiseInfoService;

    /**
     * 积分
     */
    @Autowired
    MerchandiseIntegralService merchandiseIntegralService;

    /**
     * 照片组
     */
    @Autowired
    MerchandisePhotoCordService merchandisePhotoCordService;


    /**
     * 库存
     */
    @Autowired
    MerchandiseStockService merchandiseStockService;


    /**
     * 描述
     */
    @Autowired
    MerchandiseService merchandiseService;


    //组装添加
    @Transactional
    public void  SaveAssemble(MerchanSaveVo merchanSaveVo){

        //生成UUID编号
        String uuid7 = "911"+getUUID7();
        String uuid9 = "99710629"+UUIDGenerator.getUUID9();
        //添加描述
        merchanSaveVo.getMerchandiseDetail().setDImage(String.valueOf(merchanSaveVo.getHeadImage().getResponse().getData()));
        merchanSaveVo.getMerchandiseDetail().setDId(uuid7);
        merchanSaveVo.getMerchandiseDetail().setId(Long.valueOf(uuid9));
        merchanSaveVo.getMerchandiseDetail().setDIntro(merchanSaveVo.getIntro());
        merchandiseService.SaveDetail(merchanSaveVo.getMerchandiseDetail());

        //商品详情

        merchanSaveVo.getMerchandiseInfo().setMStatus(Integer.valueOf(merchanSaveVo.getStatus()));
        merchanSaveVo.getMerchandiseInfo().setId(Long.valueOf(uuid7));
        merchanSaveVo.getMerchandiseInfo().setSnowflakeId(Long.valueOf(uuid9));
        merchandiseInfoService.SaveMerchandiseInfo(merchanSaveVo.getMerchandiseInfo());

        //库存
        merchanSaveVo.getMerchandiseStock().setSNum(Integer.valueOf(merchanSaveVo.getSnum()));
        merchandiseStockService.SaveStock(merchanSaveVo.getMerchandiseStock());

        //积分
        merchanSaveVo.getMerchandiseIntegral().setMId(Long.valueOf(uuid7));
        merchandiseIntegralService.SaveMerchandiseIntegral(merchanSaveVo.getMerchandiseIntegral());

        //照片组
        List<MerchandisePhotoCord> merchandisePhotoCordList=new ArrayList<>();
        for (int i = 0; i < merchanSaveVo.getFileList().size(); i++) {
            MerchandisePhotoCord merchange=new MerchandisePhotoCord();
            merchange.setMId(Long.valueOf(uuid7));
            merchange.setMUrlSite(merchanSaveVo.getFileList().get(i).getResponse().getData().toString());
            merchandisePhotoCordList.add(merchange);
        }
        merchandisePhotoCordService.SavePhotoCord(merchandisePhotoCordList);



        //优惠方式
        //满减
        List<MerchandiseOffers> merchandiseOffersList=new ArrayList<>();
        for (int i = 0; i < merchanSaveVo.getProductFullReductionList().size(); i++) {
            MerchandiseOffers merchandiseOffers=new MerchandiseOffers();
            merchandiseOffers.setMId(uuid7);
            merchandiseOffers.setOfferCode(20000001);
            merchandiseOffers.setFullReductionRules(Double.parseDouble(merchanSaveVo.getProductFullReductionList().get(i).getFullPrice())); //规则
            merchandiseOffers.setFullReductionDeduction(Double.parseDouble(merchanSaveVo.getProductFullReductionList().get(i).getReducePrice()));//程度
            merchandiseOffersList.add(merchandiseOffers);
        }

        //阶梯
        for (int i = 0; i < merchanSaveVo.getProductLadderList().size(); i++) {
            MerchandiseOffers merchandiseOffers=new MerchandiseOffers();
            merchandiseOffers.setMId(uuid7);
            merchandiseOffers.setOfferCode(20000002);
            merchandiseOffers.setFullReductionRules(Double.parseDouble(merchanSaveVo.getProductLadderList().get(i).getFullPrice())); //规则
            merchandiseOffers.setFullReductionDeduction(Double.parseDouble(merchanSaveVo.getProductLadderList().get(i).getReducePrice()));//程度
            merchandiseOffersList.add(merchandiseOffers);
        }
        merchandiseOffersService.SaveOffers(merchandiseOffersList);

    }


}
